package com.zhongshengnetwork.rightbe.game.activity;

import static com.zhongshengnetwork.rightbe.MainActivity.REQUEST_PERMISSION;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
//import android.support.v4.app.ActivityCompat;
//import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.dd.CircularProgressButton;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.MyUriUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.DeviceUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.HandlerDefine;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.common.customview.PublishSelectPicPopupWindow;
import com.zhongshengnetwork.rightbe.common.dialog.PayDialog;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.dbmodel.LogindbModel;
import com.zhongshengnetwork.rightbe.dbservice.LogindbService;
import com.zhongshengnetwork.rightbe.game.view.PKMenuPopWindow;
import com.zhongshengnetwork.rightbe.gsonmodel.AdvModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.HomeDataModel;
import com.zhongshengnetwork.rightbe.gsonmodel.PKMenuModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.SelfImagePreviewActivity;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.my.activity.MyCenterActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.rong.imlib.RongIMClient;
import pl.droidsonroids.gif.GifImageView;

public class GameActivity extends AppBaseActivity implements PayDialog.NetEvevt{
    private final static String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.zhongshengnetwork.rightbe";//Constant.HOST+"downapp.do?";
    private final static String content = "王者对战，好玩又益智，赶紧来体验吧~";
    private List<String> list;
    private PublishSelectPicPopupWindow menuWindow;

    private PKMenuPopWindow pkMenuPopWindow;
    public static PayDialog.NetEvevt evevt;

    private CircularProgressButton main_user_headerbt;
    private CircularProgressButton pk_bg;
    private ImageView main_user_header;
    public MyHandler myHandler;
    private Bitmap placeImg = null;
    private GifImageView home_bg_img;

    private HorizontalScrollView pk_menu_all;
    private LinearLayout game_down_menu;
    private RelativeLayout gamemenu_down_1;
    private ImageView gamemenu_down_img_1;
    private ImageView gamemenu_down_icon_1;
    private TextView gamemenu_down_text_1;
    private TextView gamemenu_down_adv_1;
    private RelativeLayout gamemenu_down_2;
    private ImageView gamemenu_down_img_2;
    private ImageView gamemenu_down_icon_2;
    private TextView gamemenu_down_text_2;
    private TextView gamemenu_down_adv_2;
    private RelativeLayout gamemenu_down_3;
    private ImageView gamemenu_down_img_3;
    private ImageView gamemenu_down_icon_3;
    private TextView gamemenu_down_text_3;
    private TextView gamemenu_down_adv_3;
    private RelativeLayout gamemenu_down_4;
    private ImageView gamemenu_down_img_4;
    private ImageView gamemenu_down_icon_4;
    private TextView gamemenu_down_text_4;
    private TextView gamemenu_down_adv_4;

    private LinearLayout game_up_menu;
    private RelativeLayout gamemenu_up_1;
    private ImageView gamemenu_up_img_1;
    private ImageView gamemenu_up_icon_1;
    private TextView gamemenu_up_text_1;
    private TextView gamemenu_up_adv_1;
    private RelativeLayout gamemenu_up_2;
    private ImageView gamemenu_up_img_2;
    private ImageView gamemenu_up_icon_2;
    private TextView gamemenu_up_text_2;
    private TextView gamemenu_up_adv_2;
    private RelativeLayout gamemenu_up_3;
    private ImageView gamemenu_up_img_3;
    private ImageView gamemenu_up_icon_3;
    private TextView gamemenu_up_text_3;
    private TextView gamemenu_up_adv_3;
    private RelativeLayout gamemenu_up_4;
    private ImageView gamemenu_up_img_4;
    private ImageView gamemenu_up_icon_4;
    private TextView gamemenu_up_text_4;
    private TextView gamemenu_up_adv_4;
    private List<HomeDataModel> homeList = null;
    private Handler handler = new Handler();
    private boolean isGetMenu = false;
    private AVLoadingIndicatorView avi;
    private boolean isAutoLogin = false;
    private RelativeLayout pk_function_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        evevt = this;
        initMenu();
        main_user_headerbt = (CircularProgressButton)findViewById(R.id.main_user_headerbt);
        main_user_headerbt.setBackgroundColor(getResources().getColor(R.color.mainColor));
        main_user_headerbt.setStrokeColor(getResources().getColor(R.color.mainColor));
        main_user_headerbt.setText("登录");
        main_user_header = (ImageView) findViewById(R.id.main_user_header);

        pk_bg = (CircularProgressButton)findViewById(R.id.pk_bg);
        pk_bg.setBackgroundColor(getResources().getColor(R.color.commonColor));
        pk_bg.setStrokeColor(getResources().getColor(R.color.commonColor));


        home_bg_img = (GifImageView) findViewById(R.id.home_bg_img);
        setLoginState();
        goToUrl();
        //注册广播
        registerBoradcastReceiver();
        myHandler = new MyHandler();
        list = new ArrayList<String>();
        list.add("每日签到");
        list.add("分享给微信好友");
        list.add("分享到微信朋友圈");
        list.add("取消");

//        //申请sd卡权限
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
//            if (ContextCompat.checkSelfPermission(GameActivity.this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED){
//                ActivityCompat.requestPermissions(GameActivity.this, new String[]{Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_IMAGES},
//                        REQUEST_PERMISSION);
//                return;
//            }
//        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
//                && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
//                    1000);
//            ToastUtil.show(GameActivity.this,"亲，请开启应用对SD卡的读写权限");
//            return;
//        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                String filePath = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.HomeImgPath,"");
                if (!CommonUtils.isEmpty(filePath)){
                    Log.e("TAG","首页图片路径是："+filePath+"\n");
                    boolean status = SPManageUtils.getInstance(CustomApplication.mContext).getSPBoolean(AdvertUtils.HomeStatus,false);
                    if (AdvertUtils.fileIsExists(filePath) && status == true){
                        home_bg_img.setImageURI(MyUriUtils.getUri(CustomApplication.mContext,filePath));
                    }
                }
            }
        },300);

    }

    private void goToUrl(){
        String url = getIntent().getStringExtra(APIKey.urlKey);
        if (!CommonUtils.isEmpty(url)){
            Intent webIntent = new Intent(GameActivity.this, MyWebActivity.class);
            webIntent.putExtra(APIKey.urlKey, url);
            startActivity(webIntent);
        }
    }

    private void initMenu(){

        pk_menu_all = (HorizontalScrollView)findViewById(R.id.pk_menu_all);
        game_down_menu = (LinearLayout)findViewById(R.id.game_down_menu);
        gamemenu_down_1 = (RelativeLayout)findViewById(R.id.gamemenu_down_1);
        gamemenu_down_img_1 = (ImageView)findViewById(R.id.gamemenu_down_img_1);
        gamemenu_down_icon_1 = (ImageView)findViewById(R.id.gamemenu_down_icon_1);
        gamemenu_down_text_1 = (TextView)findViewById(R.id.gamemenu_down_text_1);
        gamemenu_down_adv_1 = (TextView)findViewById(R.id.gamemenu_down_adv_1);
        gamemenu_down_2 = (RelativeLayout)findViewById(R.id.gamemenu_down_2);
        gamemenu_down_img_2 = (ImageView)findViewById(R.id.gamemenu_down_img_2);
        gamemenu_down_icon_2 = (ImageView)findViewById(R.id.gamemenu_down_icon_2);
        gamemenu_down_text_2 = (TextView)findViewById(R.id.gamemenu_down_text_2);
        gamemenu_down_adv_2 = (TextView)findViewById(R.id.gamemenu_down_adv_2);
        gamemenu_down_3 = (RelativeLayout)findViewById(R.id.gamemenu_down_3);
        gamemenu_down_img_3 = (ImageView)findViewById(R.id.gamemenu_down_img_3);
        gamemenu_down_icon_3 = (ImageView)findViewById(R.id.gamemenu_down_icon_3);
        gamemenu_down_text_3 = (TextView)findViewById(R.id.gamemenu_down_text_3);
        gamemenu_down_adv_3 = (TextView)findViewById(R.id.gamemenu_down_adv_3);
        gamemenu_down_4 = (RelativeLayout)findViewById(R.id.gamemenu_down_4);
        gamemenu_down_img_4 = (ImageView)findViewById(R.id.gamemenu_down_img_4);
        gamemenu_down_icon_4 = (ImageView)findViewById(R.id.gamemenu_down_icon_4);
        gamemenu_down_text_4 = (TextView)findViewById(R.id.gamemenu_down_text_4);
        gamemenu_down_adv_4 = (TextView)findViewById(R.id.gamemenu_down_adv_4);

        game_up_menu = (LinearLayout)findViewById(R.id.game_up_menu);
        gamemenu_up_1 = (RelativeLayout)findViewById(R.id.gamemenu_up_1);
        gamemenu_up_img_1 = (ImageView)findViewById(R.id.gamemenu_up_img_1);
        gamemenu_up_icon_1 = (ImageView)findViewById(R.id.gamemenu_up_icon_1);
        gamemenu_up_text_1 = (TextView)findViewById(R.id.gamemenu_up_text_1);
        gamemenu_up_adv_1 = (TextView)findViewById(R.id.gamemenu_up_adv_1);
        gamemenu_up_2 = (RelativeLayout)findViewById(R.id.gamemenu_up_2);
        gamemenu_up_img_2 = (ImageView)findViewById(R.id.gamemenu_up_img_2);
        gamemenu_up_icon_2 = (ImageView)findViewById(R.id.gamemenu_up_icon_2);
        gamemenu_up_text_2 = (TextView)findViewById(R.id.gamemenu_up_text_2);
        gamemenu_up_adv_2 = (TextView)findViewById(R.id.gamemenu_up_adv_2);
        gamemenu_up_3 = (RelativeLayout)findViewById(R.id.gamemenu_up_3);
        gamemenu_up_img_3 = (ImageView)findViewById(R.id.gamemenu_up_img_3);
        gamemenu_up_icon_3 = (ImageView)findViewById(R.id.gamemenu_up_icon_3);
        gamemenu_up_text_3 = (TextView)findViewById(R.id.gamemenu_up_text_3);
        gamemenu_up_adv_3 = (TextView)findViewById(R.id.gamemenu_up_adv_3);
        gamemenu_up_4 = (RelativeLayout)findViewById(R.id.gamemenu_up_4);
        gamemenu_up_img_4 = (ImageView)findViewById(R.id.gamemenu_up_img_4);
        gamemenu_up_icon_4 = (ImageView)findViewById(R.id.gamemenu_up_icon_4);
        gamemenu_up_text_4 = (TextView)findViewById(R.id.gamemenu_up_text_4);
        gamemenu_up_adv_4 = (TextView)findViewById(R.id.gamemenu_up_adv_4);
        avi = (AVLoadingIndicatorView)findViewById(R.id.avi);
        pk_function_layout = (RelativeLayout) findViewById(R.id.pk_function_layout);

        new Thread() {
            @Override
            public void run() {
                super.run();
                String homeData = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.HomeData,"");
                if (!CommonUtils.isEmpty(homeData)){
                    setHomeView(homeData);
                }else {
                    final HashMap<String, String> map = new HashMap<String, String>();
                    if (CommonUtils.isLogin() == true){
                        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                    }else {
                        map.put(APIKey.tokenKey, "1");
                    }
                    String url = "miniapp/appid/getsubjectname.do";
                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            final CommonModel commonModel = GsonTools.getCommonModel(data);
                            if (commonModel.getFlag().equals("1")){
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.HomeData,data);
                                setHomeView(data);
                            }
                        }

                        @Override
                        public void onError(String msg) {

                        }
                    });
                }

            }
        }.start();

        autoLogin();

    }

    private void autoLogin(){
        if(CustomApplication.loginModel != null && !CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
            return;
        }
        if (isAutoLogin == true){
            return;
        }
        isAutoLogin = true;

        new Thread() {
            @Override
            public void run() {
                super.run();
                String mobileid = DeviceUtils.getDeviceID();
                if (mobileid == null || mobileid.length() == 0){
                    return;
                }
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put("mobileid", mobileid);
                String url = "api/user/autologin.do";
                Log.e("TAG","重新获取token\n");
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        isAutoLogin = false;
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            final LogindbModel model = GsonTools.getThirdLogindbModel(data);
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (CustomApplication.loginModel == null){
                                        CustomApplication.loginModel = new LogindbModel();
                                        CustomApplication.loginModel.setUid("1");//默认用户id
                                    }
                                    System.out.print("~rctoken~:"+model.getRctoken()+"\n");
                                    CustomApplication.loginModel.setRctoken(model.getRctoken());
                                    CustomApplication.loginModel.setToken(model.getToken());
                                    CustomApplication.isAutoLogin = true;
                                    Log.e("TAG","获取的token="+CustomApplication.loginModel.getToken());
                                    CustomApplication.loginModel.setExpires(model.getExpires());
                                    CustomApplication.loginModel.setUid(model.getUid());
                                    CustomApplication.loginModel.setHeader(model.getHeader());
                                    CustomApplication.loginModel.setThumbheader(model.getThumbheader());
                                    CustomApplication.loginModel.setBackground(model.getBackground());
                                    CustomApplication.loginModel.setNickname(model.getNickname());
                                    CustomApplication.loginModel.setSex(model.getSex());
                                    CustomApplication.loginModel.setSignature(model.getSignature());
                                    CustomApplication.loginModel.setLocation(model.getLocation());
                                    CustomApplication.loginModel.setUserauth(model.getUserauth());
                                    CustomApplication.loginModel.setPayauth(model.getPayauth());
                                    CustomApplication.loginModel.setWeiJuId(model.getWeiJuId());
                                    CustomApplication.loginModel.setMobile(model.getMobile());
                                    CustomApplication.loginModel.setNeed(model.getNeed());
                                    LogindbService.deleteLoginModel();
                                    LogindbService.saveOrUpdateLogindbMode(CustomApplication.loginModel);
                                }
                            },10);

                        }
                    }

                    @Override
                    public void onError(String msg) {
                        isAutoLogin = false;
                    }
                });
            }
        }.start();

    }

    private void setHomeView(final String data){


        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                /**
                 *要执行的操作
                 */
                avi.hide();
                if (!CommonUtils.isEmpty(data)){
                    homeList = GsonTools.getHomeDataModel(data);
                    if (homeList != null && homeList.size() > 0){
                        if (homeList.size() > 6){
                            for (int i = 0; i < homeList.size();i++){
                                HomeDataModel m = homeList.get(i);
                                if (i == 0){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_1.setVisibility(View.GONE);
                                        gamemenu_up_text_1.setVisibility(View.GONE);
                                        gamemenu_up_adv_1.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_1);
                                    }else {
                                        gamemenu_up_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_1.setVisibility(View.GONE);
                                        gamemenu_up_img_1.setVisibility(View.GONE);
                                        gamemenu_up_text_1.setText(m.getName());
                                        gamemenu_up_icon_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_1);
                                        }
                                    }
                                }else if (i == 1){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_2.setVisibility(View.GONE);
                                        gamemenu_up_text_2.setVisibility(View.GONE);
                                        gamemenu_up_adv_2.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_2);
                                    }else {
                                        gamemenu_up_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_2.setVisibility(View.GONE);
                                        gamemenu_up_img_2.setVisibility(View.GONE);
                                        gamemenu_up_text_2.setText(m.getName());
                                        gamemenu_up_icon_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_2);
                                        }
                                    }
                                }else if (i == 2){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_3.setVisibility(View.GONE);
                                        gamemenu_up_text_3.setVisibility(View.GONE);
                                        gamemenu_up_adv_3.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_3);
                                    }else {
                                        gamemenu_up_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_3.setVisibility(View.GONE);
                                        gamemenu_up_img_3.setVisibility(View.GONE);
                                        gamemenu_up_text_3.setText(m.getName());
                                        gamemenu_up_icon_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_3);
                                        }
                                    }
                                }else if (i == 3){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_4.setVisibility(View.GONE);
                                        gamemenu_up_text_4.setVisibility(View.GONE);
                                        gamemenu_up_adv_4.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_4);
                                    }else {
                                        gamemenu_up_4.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_4.setVisibility(View.GONE);
                                        gamemenu_up_img_4.setVisibility(View.GONE);
                                        gamemenu_up_text_4.setText(m.getName());
                                        gamemenu_up_icon_4.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_4);
                                        }
                                    }
                                }else if (i == 4){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_1.setVisibility(View.GONE);
                                        gamemenu_down_text_1.setVisibility(View.GONE);
                                        gamemenu_down_adv_1.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_1);
                                    }else {
                                        gamemenu_down_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_1.setVisibility(View.GONE);
                                        gamemenu_down_img_1.setVisibility(View.GONE);
                                        gamemenu_down_text_1.setText(m.getName());
                                        gamemenu_down_icon_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_1);
                                        }
                                    }
                                }else if (i == 5){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_2.setVisibility(View.GONE);
                                        gamemenu_down_text_2.setVisibility(View.GONE);
                                        gamemenu_down_adv_2.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_2);
                                    }else {
                                        gamemenu_down_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_2.setVisibility(View.GONE);
                                        gamemenu_down_img_2.setVisibility(View.GONE);
                                        gamemenu_down_text_2.setText(m.getName());
                                        gamemenu_down_icon_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_2);
                                        }
                                    }
                                }else if (i == 6){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_3.setVisibility(View.GONE);
                                        gamemenu_down_text_3.setVisibility(View.GONE);
                                        gamemenu_down_adv_3.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_3);
                                    }else {
                                        gamemenu_down_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_3.setVisibility(View.GONE);
                                        gamemenu_down_img_3.setVisibility(View.GONE);
                                        gamemenu_down_text_3.setText(m.getName());
                                        gamemenu_down_icon_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_3);
                                        }
                                    }
                                }else if (i == 7){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_4.setVisibility(View.GONE);
                                        gamemenu_down_text_4.setVisibility(View.GONE);
                                        gamemenu_down_adv_4.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_4);
                                    }else {
                                        gamemenu_down_4.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_4.setVisibility(View.GONE);
                                        gamemenu_down_img_4.setVisibility(View.GONE);
                                        gamemenu_down_text_4.setText(m.getName());
                                        gamemenu_down_icon_4.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_4);
                                        }
                                    }
                                }
                            }
                            if (homeList.size() == 7){
                                //gamemenu_down_1.setVisibility(View.INVISIBLE);
                                //gamemenu_down_2.setVisibility(View.INVISIBLE);
                                //gamemenu_down_3.setVisibility(View.INVISIBLE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                //gamemenu_up_4.setVisibility(View.INVISIBLE);
                            }
                        }else {
                            for (int i = 0; i < homeList.size();i++){
                                HomeDataModel m = homeList.get(i);
                                if (i == 0){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_1.setVisibility(View.GONE);
                                        gamemenu_up_text_1.setVisibility(View.GONE);
                                        gamemenu_up_adv_1.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_1);
                                    }else {
                                        gamemenu_up_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_1.setVisibility(View.GONE);
                                        gamemenu_up_img_1.setVisibility(View.GONE);
                                        gamemenu_up_text_1.setText(m.getName());
                                        gamemenu_up_icon_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_1);
                                        }
                                    }
                                }else if (i == 1){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_2.setVisibility(View.GONE);
                                        gamemenu_up_text_2.setVisibility(View.GONE);
                                        gamemenu_up_adv_2.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_2);
                                    }else {
                                        gamemenu_up_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_2.setVisibility(View.GONE);
                                        gamemenu_up_img_2.setVisibility(View.GONE);
                                        gamemenu_up_text_2.setText(m.getName());
                                        gamemenu_up_icon_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_2);
                                        }
                                    }
                                }else if (i == 2){
                                    if (m.getName().equals("广告")){
                                        gamemenu_up_icon_3.setVisibility(View.GONE);
                                        gamemenu_up_text_3.setVisibility(View.GONE);
                                        gamemenu_up_adv_3.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_img_3);
                                    }else {
                                        gamemenu_up_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_up_adv_3.setVisibility(View.GONE);
                                        gamemenu_up_img_3.setVisibility(View.GONE);
                                        gamemenu_up_text_3.setText(m.getName());
                                        gamemenu_up_icon_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_up_icon_3);
                                        }
                                    }
                                }else if (i == 3){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_1.setVisibility(View.GONE);
                                        gamemenu_down_text_1.setVisibility(View.GONE);
                                        gamemenu_down_adv_1.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_1);
                                    }else {
                                        gamemenu_down_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_1.setVisibility(View.GONE);
                                        gamemenu_down_img_1.setVisibility(View.GONE);
                                        gamemenu_down_text_1.setText(m.getName());
                                        gamemenu_down_icon_1.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_1);
                                        }
                                    }
                                }else if (i == 4){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_2.setVisibility(View.GONE);
                                        gamemenu_down_text_2.setVisibility(View.GONE);
                                        gamemenu_down_adv_2.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_2);
                                    }else {
                                        gamemenu_down_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_2.setVisibility(View.GONE);
                                        gamemenu_down_img_2.setVisibility(View.GONE);
                                        gamemenu_down_text_2.setText(m.getName());
                                        gamemenu_down_icon_2.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_2);
                                        }
                                    }
                                }else if (i == 5){
                                    if (m.getName().equals("广告")){
                                        gamemenu_down_icon_3.setVisibility(View.GONE);
                                        gamemenu_down_text_3.setVisibility(View.GONE);
                                        gamemenu_down_adv_3.setText(m.getName());
                                        Glide.with(GameActivity.this).load(m.getBgurl()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_img_3);
                                    }else {
                                        gamemenu_down_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        gamemenu_down_adv_3.setVisibility(View.GONE);
                                        gamemenu_down_img_3.setVisibility(View.GONE);
                                        gamemenu_down_text_3.setText(m.getName());
                                        gamemenu_down_icon_3.setBackgroundColor(Color.parseColor("#"+m.getBgcolor()));
                                        if (!CommonUtils.isEmpty(m.getIcon())){
                                            Glide.with(GameActivity.this).load(m.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(gamemenu_down_icon_3);
                                        }
                                    }
                                }
                            }
                            if (homeList.size() == 1){
                                gamemenu_down_1.setVisibility(View.GONE);
                                gamemenu_down_2.setVisibility(View.GONE);
                                gamemenu_down_3.setVisibility(View.GONE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_2.setVisibility(View.GONE);
                                gamemenu_up_3.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 2) {
                                gamemenu_down_1.setVisibility(View.GONE);
                                gamemenu_down_2.setVisibility(View.GONE);
                                gamemenu_down_3.setVisibility(View.GONE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_3.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 3){
                                gamemenu_down_1.setVisibility(View.GONE);
                                gamemenu_down_2.setVisibility(View.GONE);
                                gamemenu_down_3.setVisibility(View.GONE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 4){
                                //gamemenu_down_1.setVisibility(View.INVISIBLE);
                                gamemenu_down_2.setVisibility(View.GONE);
                                gamemenu_down_3.setVisibility(View.GONE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 5){
                                //gamemenu_down_1.setVisibility(View.INVISIBLE);
                                //gamemenu_down_2.setVisibility(View.INVISIBLE);
                                gamemenu_down_3.setVisibility(View.GONE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 6){
                                //gamemenu_down_1.setVisibility(View.INVISIBLE);
                                //gamemenu_down_2.setVisibility(View.INVISIBLE);
                                //gamemenu_down_3.setVisibility(View.INVISIBLE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                gamemenu_up_4.setVisibility(View.GONE);
                            }else if (homeList.size() == 7){
                                //gamemenu_down_1.setVisibility(View.INVISIBLE);
                                //gamemenu_down_2.setVisibility(View.INVISIBLE);
                                //gamemenu_down_3.setVisibility(View.INVISIBLE);
                                gamemenu_down_4.setVisibility(View.GONE);
                                //gamemenu_up_4.setVisibility(View.INVISIBLE);
                            }
                        }
                        pk_menu_all.setVisibility(View.VISIBLE);
                        game_down_menu.setVisibility(View.VISIBLE);
                        game_up_menu.setVisibility(View.VISIBLE);
                    }

                    List<AdvModel> list = GsonTools.getAdvModel(data);
                    if (list != null && list.size() > 0){
                        for (AdvModel model : list){
                            if (model.getType() == 0){//启动广告
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.AdvBgUrl,model.getBgurl());
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.AdvStatus,model.isStatus());
                                if (CommonUtils.isEmpty(model.getContenturl())==false) {
                                    SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.AdvContentUrl, model.getContenturl());
                                }
                                loadAdvertImg(model.getBgurl(),AdvertUtils.AdvImgPath);
                            }else if (model.getType() == 1){//首页
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.HomeBgUrl,model.getBgurl());
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.HomeStatus,model.isStatus());
                                if (CommonUtils.isEmpty(model.getContenturl())==false){
                                    SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.HomeContentUrl,model.getContenturl());
                                }
                                loadAdvertImg(model.getBgurl(),AdvertUtils.HomeImgPath);
                            }
                        }
                    }
                    animationOut();
                }
            }
        }, 10);//3秒后执行Runnable中的run方法

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("TAG","可视了\n");
        Message msg = new Message();
        msg.obj = new String(HandlerDefine.login_msg);
        //myHandler.sendMessage(msg);
        myHandler.sendMessageDelayed(msg,100);
        autoLogin();
    }

    public void onClickMenu(View view){
        Log.e("TAG","点击了菜单\n");
        HomeDataModel model = null;
        switch (view.getId()){
            case R.id.gamemenu_down_1:
                if (homeList.size()>6){
                    model = homeList.get(4);
                }else {
                    model = homeList.get(3);
                }
                break;
            case R.id.gamemenu_down_2:
                if (homeList.size()>6){
                    model = homeList.get(5);
                }else {
                    model = homeList.get(4);
                }
                break;
            case R.id.gamemenu_down_3:
                if (homeList.size()>6){
                    model = homeList.get(6);
                }else {
                    model = homeList.get(5);
                }
                break;
            case R.id.gamemenu_down_4:
                if (homeList.size()>6){
                    model = homeList.get(7);
                }else {
                    model = homeList.get(6);
                }
                break;
            case R.id.gamemenu_up_1:
                model = homeList.get(0);
                break;
            case R.id.gamemenu_up_2:
                model = homeList.get(1);
                break;
            case R.id.gamemenu_up_3:
                model = homeList.get(2);
                break;
            case R.id.gamemenu_up_4:
                model = homeList.get(3);
                break;
        }
        dealWithMenu(model);
    }

    public void dealWithMenu(HomeDataModel model){
        if (model != null){
            if (CustomApplication.loginModel == null){
                CustomApplication.loginModel = new LogindbModel();
                CustomApplication.loginModel.setUid("1");//默认用户id
            }
            if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
                autoLogin();
                //startActivity(new Intent(GameActivity.this, LoginActivity.class));
                return;
            }
            if (model.getAppid().equals("appid")){//成语猜猜
                startActivity(new Intent(GameActivity.this, GuessWordActivity.class));
            }else if (model.getAppid().equals("appidlianlian")){//成语消消
                startActivity(new Intent(GameActivity.this, CancleWordActivity.class));
            }else if (model.getAppid().equals("linkwordappid")){//成语接龙
                Intent intent = new Intent(GameActivity.this, LinkWordActivity.class);
                intent.putExtra("model",model);
                startActivity(intent);
            }else if (model.getAppid().equals("smartwordappid")){//智多多
                Intent intent = new Intent(GameActivity.this, HeaderWordActivity.class);
                intent.putExtra("model",model);
                startActivity(intent);
            }else if (model.getAppid().contains("subject")){//综合题库
                Intent intent = new Intent(GameActivity.this, SubjectWordActivity.class);
                intent.putExtra("model",model);
                startActivity(intent);
            }else if(model.getAppid().contains("logicalappid")){
                Intent intent = new Intent(GameActivity.this, LogicalSubjectActivity.class);
                intent.putExtra("model",model);
                startActivity(intent);
            } else if (model.getName().equals("广告")){
                Intent webIntent = new Intent(GameActivity.this, MyWebActivity.class);
                webIntent.putExtra(APIKey.urlKey, model.getContenturl());
                startActivity(webIntent);
            }else {
                ToastUtil.show(GameActivity.this,"亲，该功能需要升级到最新版本才能体验哦~");
            }
        }
    }

    public void onClickBg(View view){
        Log.e("TAG","点击了首页背景\n");
        boolean status = SPManageUtils.getInstance(CustomApplication.mContext).getSPBoolean(AdvertUtils.HomeStatus,false);
        if (status == true){
            String url = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.HomeContentUrl,"");
            if (!CommonUtils.isEmpty(url)){
                Log.e("TAG","背景链接是："+url+"" + "\n");
                if (url.contains("appfunction:")){
                    String index = url.substring(url.indexOf(":")+1,url.length());
                    if (homeList != null && homeList.size() > 0){
                        HomeDataModel model = homeList.get(Integer.valueOf(index));
                        dealWithMenu(model);
                    }
                }else {
                    Intent webIntent = new Intent(GameActivity.this, MyWebActivity.class);
                    webIntent.putExtra(APIKey.urlKey, url);
                    startActivity(webIntent);
                }

            }

        }
    }

    public void onClickLogin(View view){
        if (CustomApplication.loginModel == null){
            CustomApplication.loginModel = new LogindbModel();
            CustomApplication.loginModel.setUid("1");//默认用户id
        }
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
            startActivity(new Intent(GameActivity.this, LoginActivity.class));
            return;
        }
        if (CustomApplication.isAutoLogin == true){
            startActivity(new Intent(GameActivity.this, LoginActivity.class));
            return;
        }
        startActivity(new Intent(GameActivity.this, MyCenterActivity.class));
    }

    public void onClickPK(View view){
        Log.e("TAG","点击了PK");
        if (CustomApplication.loginModel == null){
            CustomApplication.loginModel = new LogindbModel();
            CustomApplication.loginModel.setUid("1");//默认用户id
        }
        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
            startActivity(new Intent(GameActivity.this, LoginActivity.class));
            return;
        }
        if (CustomApplication.isAutoLogin == true){
            startActivity(new Intent(GameActivity.this, LoginActivity.class));
            return;
        }
        //startActivity(new Intent(GameActivity.this, PKMenuActivity.class));
        getMenu(view);
    }

    public void setLoginState(){

        if (CustomApplication.loginModel == null){
            CustomApplication.loginModel = new LogindbModel();
            CustomApplication.loginModel.setUid("1");//默认用户id
        }

        if (CommonUtils.isEmpty(CustomApplication.loginModel.getToken())){
            main_user_headerbt.setVisibility(View.VISIBLE);
            main_user_header.setVisibility(View.INVISIBLE);
        }else {
            if (CustomApplication.isAutoLogin == true){
                main_user_headerbt.setVisibility(View.VISIBLE);
                main_user_header.setVisibility(View.INVISIBLE);
            }else {
                main_user_headerbt.setVisibility(View.INVISIBLE);
                main_user_header.setVisibility(View.VISIBLE);
                //System.out.print("头像是："+CustomApplication.loginModel.getHeader()+"\n");
                Glide.with(GameActivity.this).load(CustomApplication.loginModel.getHeader()).skipMemoryCache(true).transform(new CircleTransform(GameActivity.this)).dontAnimate().into(main_user_header);
                //Glide.with(this).load("https://image.daydayrise.net/cy10.jpg").skipMemoryCache(true).transform(new CircleTransform(this.getApplicationContext())).dontAnimate().into(main_user_header);
                //System.out.print("头像是加载完毕"+"\n");
                Log.e("TAG","头像是token="+CustomApplication.loginModel.getToken());
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        //2018年11月10日屏蔽
                        //初始化融云
                        //CustomApplication.customApplication.initRongIMClient();
                    }
                }.start();
            }


        }

    }

    class MyHandler extends Handler {
        //默认构造函数
        public MyHandler(){
            super();
        }

        //带有Looper参数的构造函数
        public MyHandler(Looper L){
            super(L);
        }

        //子类重写次方法，处理消息

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg == null || msg.obj == null){
                return;
            }
            if (msg.obj.toString().equals(HandlerDefine.login_msg)){
                setLoginState();
            }else if (msg.obj.toString().equals(HandlerDefine.logout_msg)){
                setLoginState();
            }
        }
    }

    /**
     * 广播接收者
     * @author Gordon
     */
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.print("onReceive接收到广播了" + "\n");
            String action = intent.getAction();
            if (action.equals(BroadcastDefine.Login_Success_Action)){
                Message msg = new Message();
                msg.obj = new String(HandlerDefine.login_msg);
                //myHandler.sendMessage(msg);
                //myHandler.sendMessageDelayed(msg,500);
                //new getImageCacheAsyncTask(GameActivity.this).execute(CustomApplication.loginModel.getThumbheader());
            }else if (action.equals(BroadcastDefine.Logout_Success_Action)) {//退出登录
                //RongIMClient.getInstance().disconnect();//2018年11月10日屏蔽
                CustomApplication.isSign = false;
                CustomApplication.loginModel.setToken(null);
                LogindbService.deleteLoginModel();
                Message msg = new Message();
                msg.obj = new String(HandlerDefine.logout_msg);
                //myHandler.sendMessage(msg);
            }
        }
    };

    /**
     * 注册广播
     * @author Gordon
     */
    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        //添加接收广播类型
        myIntentFilter.addAction(BroadcastDefine.Login_Success_Action);
        myIntentFilter.addAction(BroadcastDefine.Logout_Success_Action);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private class getImageCacheAsyncTask extends AsyncTask<String, Void, File> {
        private final Context context;

        public getImageCacheAsyncTask(Context context) {
            this.context = context;
        }

        @Override
        protected File doInBackground(String... params) {
            String imgUrl =  params[0];
            try {
                return Glide.with(context)
                        .load(imgUrl)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get();
            } catch (Exception ex) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(File result) {
            if (result == null) {
                return;
            }
            //此path就是对应文件的缓存路径
            String path = result.getPath();
            Log.e("path", path+"\n");
            placeImg = BitmapFactory.decodeFile(path);
            Message msg = new Message();
            msg.obj = new String(HandlerDefine.login_msg);
            myHandler.sendMessage(msg);
        }
    }

    private void getMenu(final View view){
        if (isGetMenu == true){
            return;
        }
        avi.show();
        isGetMenu = true;
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+"pkappid"+"/pkmenu.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                Log.e("TAG","数据="+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    pkMenuPopWindow = new PKMenuPopWindow(GameActivity.this,itemsOnClick);
                    pkMenuPopWindow.showMenu(data);
                    pkMenuPopWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
                isGetMenu = false;
                avi.hide();
            }
            @Override
            public void onError(String msg) {
                isGetMenu = false;
                avi.hide();
            }
        });
    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            Log.e("TAG","点击了\n");
            PKMenuModel pkMenuModel = null;
            switch (v.getId()) {
                case R.id.pk_menu_pop_startpk:
                    pkMenuModel = pkMenuPopWindow.pkMenuList.get(0);
                    break;
                case R.id.pk_menu_pop_middlepk:
                    pkMenuModel = pkMenuPopWindow.pkMenuList.get(1);
                    break;
                case R.id.pk_menu_pop_highpk:
                    pkMenuModel = pkMenuPopWindow.pkMenuList.get(2);
                    break;
                case R.id.pk_menu_pop_facepk:
                    pkMenuModel = pkMenuPopWindow.pkMenuList.get(3);
                    break;
                case R.id.pk_menu_pop_rangepk:
                    pkMenuModel = pkMenuPopWindow.pkMenuList.get(4);
                    break;
                case R.id.getgold:
                    pkMenuPopWindow.dismiss();
                    Log.e("TAG","点击了分享\n");
                    share(v);
                    break;
                case R.id.pk_menu_pop_close:
                    pkMenuPopWindow.dismiss();
                    break;
                default:
                    break;
            }
            if (pkMenuModel != null){
                if (pkMenuModel.getType() == 4){
                    Intent intent = new Intent(GameActivity.this,GameRangeActivity.class);
                    intent.putExtra("appid","pkappid");
                    startActivity(intent);
                }else if (pkMenuModel.getType() >= 1 && pkMenuModel.getType() <= 3){
                    if (pkMenuModel.isStatus() == false){
                        return;
                    }
                    if (pkMenuModel.getGoldcount() > pkMenuPopWindow.pkUserModel.getGoldcount()){
                        ToastUtil.show(GameActivity.this,"亲，你金币不足哦");
                        return;
                    }
                    Intent intent = new Intent(GameActivity.this, PrePKActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("pkuser", pkMenuPopWindow.pkUserModel);//序列化
                    intent.putExtras(bundle);//发送数据
                    intent.putExtra("fee",pkMenuModel.getGoldcount());
                    intent.putExtra("type",pkMenuModel.getType());
                    intent.putExtra("code", CommonUtils.formatString(pkMenuPopWindow.code));
                    startActivity(intent);
                }else if (pkMenuModel.getType() == 0){
                    pkMenuPopWindow.mPKMenuModel = pkMenuModel;
                    PayDialog payDialog = new PayDialog(GameActivity.this);
                    payDialog.show();
                }
            }

        }
    };

    //面对面对战
    @Override
    public void onTextFinish(String text) {
        pkMenuPopWindow.code = text;
        Intent intent = new Intent(GameActivity.this, PrePKActivity.class);
        Bundle bundle=new Bundle();
        bundle.putSerializable("pkuser", pkMenuPopWindow.pkUserModel);//序列化
        intent.putExtras(bundle);//发送数据
        intent.putExtra("fee",pkMenuPopWindow.mPKMenuModel.getGoldcount());
        intent.putExtra("type",pkMenuPopWindow.mPKMenuModel.getType());
        intent.putExtra("code",CommonUtils.formatString(pkMenuPopWindow.code));
        startActivity(intent);
    }

    private void signDo(final View v){
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        String url = "miniapp/"+"pkappid"+"/usersign.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {// usersign
            @Override
            public void onSuccess(String data) {
                Log.e("TAG",data);
                final CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    ToastUtil.show(GameActivity.this,commonModel.getMsg());
                    getMenu(v);
                }else {
                    ToastUtil.show(GameActivity.this,"你已领取");
                }
            }
            @Override
            public void onError(String msg) {

            }
        });
    }

    private void share(View v){
        menuWindow = new PublishSelectPicPopupWindow(GameActivity.this,itemsOnClickA,list);
        // 显示窗口
        menuWindow.showAtLocation(v,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    // 为弹出窗口实现监听类
    private View.OnClickListener itemsOnClickA = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.first_button:
                    signDo(v);
                    break;
                case R.id.second_button://分享给微信好友
                    shareToWX(1,url,pkMenuPopWindow.pkShareModel.getName(),content);
                    break;
                case R.id.third_button://分享到微信朋友圈
                    shareToWX(0,url,pkMenuPopWindow.pkShareModel.getName(),content);
                    break;
                default:
                    break;
            }

        }
    };

    //分享到微信
    private void shareToWX(int type,String url,String title,String content){
        //初始化一个WXWebpageObject对象，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //用WXWebpageObject对象初始化一个WXMediaMessage对象，填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = content;
        Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.mipmap.icon);
        msg.thumbData = CommonUtils.bitmapToByteArray(thumb,true);
        //构成一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        req.scene = type==0 ? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
        CustomApplication.mWxApi.sendReq(req);

    }


    //下载图片
    private void loadAdvertImg(final String advertUrl, final String key) {
        if (!TextUtils.isEmpty(advertUrl)) {
            final String[] split = advertUrl.split("/");
            File advert = CustomApplication.mContext.getExternalFilesDir("advert");
            //final File file = new File(advert, System.currentTimeMillis() + split[split.length - 1]);
            final File file = new File(advert,  split[split.length - 1]);
            if (AdvertUtils.fileIsExists(file.toString())){
                Log.e("file", "文件存在了："+file.toString());
            }else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("file", "保存路径："+file.toString());
                        try {
                            AdvertUtils.onSaveBitmap(advertUrl, file.toString(),CustomApplication.mContext,key);
                            removeFootList(file.toString(),key);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }

        } else {
            // 获取服务器路径为空时，保存空本地路径，则不显示广告图
            removeFootList("",AdvertUtils.AdvImgPath);
        }

    }

    private void removeFootList(String filePath, String key) {
        String oldFilePath = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(key,"");
        if (!CommonUtils.isEmpty(oldFilePath) && !oldFilePath.equals(filePath)){
            if (AdvertUtils.fileIsExists(oldFilePath)) {
                File f = new File(oldFilePath);
                f.delete();
            }
        }
    }

    private void animationOut(){
        Animation animation = AnimationUtils.loadAnimation(GameActivity.this, R.anim.pushfromdown);
        pk_function_layout.startAnimation(animation);
        animation = AnimationUtils.loadAnimation(GameActivity.this, R.anim.pushfromleft);
        pk_menu_all.startAnimation(animation);
    }

}

